package demo2;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LianBao
 * Date: 2024-03-31
 * Time: 16:59
 */
public class arrayQueue {
    private int[] elem;//数组
    private int first;
    private int last;

    public arrayQueue() {
        this.elem = new int[10];
    }

    //表示让last或者first到下一个位置,不能first++,因为长度限定,一直++会越界
    //last = (last+1)%length
    //first = (first+1)%length
    public void offer(int val) {
        //尾插
        if (((last + 1) % elem.length) == first) {
            //如果满了,不能插
            return;
        }
        elem[last] = val;
        last = (last + 1) % elem.length;
    }

    //头删
    public int poll() {
        if (empty()) {
            //为空
            return -1;
        }
        int ret = elem[first];
        first = (first + 1) % elem.length;
        return ret;
    }

    public int peek() {
        if (empty()) {
            //为空
            return -1;
        }

        return elem[first];
    }

    public boolean empty() {
        return this.last == this.first;
    }

}
